package com.ruoyi.specific.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.specific.domain.ImprovingInstance;
import com.ruoyi.specific.domain.bo.ImprovingInstanceBo;
import com.ruoyi.specific.domain.vo.ImprovingInstanceVo;
import com.ruoyi.specific.mapper.ImprovingInstanceMapper;
import com.ruoyi.specific.service.IImprovingInstanceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 持续改善实例Service业务层处理
 *
 * @author ruoyi
 * @date 2024-02-27
 */
@RequiredArgsConstructor
@Service
public class ImprovingInstanceServiceImpl implements IImprovingInstanceService {

    private final ImprovingInstanceMapper baseMapper;

    /**
     * 查询持续改善实例
     */
    @Override
    public ImprovingInstanceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询持续改善实例
     */
    @Override
    public List<ImprovingInstanceVo> queryByIds(Collection<Long> ids){
        return baseMapper.selectVoBatchIds(ids);
    }

    /**
     * 查询持续改善实例列表
     */
    @Override
    public TableDataInfo<ImprovingInstanceVo> queryPageList(ImprovingInstanceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ImprovingInstance> lqw = buildQueryWrapper(bo);
        Page<ImprovingInstanceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询持续改善实例列表
     */
    @Override
    public List<ImprovingInstanceVo> queryList(ImprovingInstanceBo bo) {
        LambdaQueryWrapper<ImprovingInstance> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ImprovingInstance> buildQueryWrapper(ImprovingInstanceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ImprovingInstance> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, ImprovingInstance::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), ImprovingInstance::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getKind()), ImprovingInstance::getKind, bo.getKind());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), ImprovingInstance::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), ImprovingInstance::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getDesPicture()), ImprovingInstance::getDesPicture, bo.getDesPicture());
        lqw.eq(StringUtils.isNotBlank(bo.getSuggestion()), ImprovingInstance::getSuggestion, bo.getSuggestion());
        lqw.eq(StringUtils.isNotBlank(bo.getExpect()), ImprovingInstance::getExpect, bo.getExpect());
        lqw.eq(StringUtils.isNotBlank(bo.getExpPicture()), ImprovingInstance::getExpPicture, bo.getExpPicture());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ImprovingInstance::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getAttachment()), ImprovingInstance::getAttachment, bo.getAttachment());
        lqw.between(params.get("beginDate") != null && params.get("endDate") != null,
            ImprovingInstance::getDate ,params.get("beginDate"), params.get("endDate"));
        if (bo.getUserIdList() != null) {
            lqw.in(ImprovingInstance::getUserId, Arrays.asList(bo.getUserIdList()));
        }
        return lqw;
    }

    /**
     * 新增持续改善实例
     */
    @Override
    public Boolean insertByBo(ImprovingInstanceBo bo) {
        ImprovingInstance add = BeanUtil.toBean(bo, ImprovingInstance.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改持续改善实例
     */
    @Override
    public Boolean updateByBo(ImprovingInstanceBo bo) {
        ImprovingInstance update = BeanUtil.toBean(bo, ImprovingInstance.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ImprovingInstance entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除持续改善实例
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
